package xx.services.entity.rest;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MultivaluedMap;

import xx.entity.bas.Attach;
import xx.services.Passport;
import xx.services.entity.inter.IAttachInterface;
import xx.services.util.Constants;
import xx.util.Arr;
import xx.util.string.Format;

/**
 * 附件服务接口
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/attach")
 * @author dengjinye,2010-12-18 $LastChangedDate$ $Author$
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/attach")
public class AttachRest extends Passport implements IAttachInterface
{

    public AttachRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    /**
     * 删除附件{attach_id}集。
     * 
     * @DELETE
     * @Path ("/{attach_id}")
     * @param attachId
     *            多个ID用“_”连接起来
     * @return
     * @author dengjinye,2010-10-29
     */
    @DELETE
    @Path("/{attach_id}")
    public String deleteAttach(@PathParam("attach_id")
    String attachId)
    {
        try
        {
            long[] attachIdArr = Arr.toLongArray(attachId, "_");
            for (long id : attachIdArr)
            {
                Attach attach = new Attach(qr, owner);
                attach.setAttachId(id);
                attach.delete();
                addUrlLog(String.valueOf(attach.getAttachId()),String.valueOf(attach.getEntityId()));
            }
            return ok(true);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 获取附件详细内容。
     * 
     * @GET
     * @Path ("/{attach_id}")
     * @param attachId
     *            -@PathParam("attach_id") 附件ID
     * @return 附件实体
     * @author dengjinye,2010-10-29
     */
    @GET
    @Path("/{attach_id}")
    public String getAttach(@PathParam("attach_id")
    long attachId)
    {
        try
        {
            Attach attach = new Attach(qr, owner);
            attach.setAttachId(attachId);
            attach.load();
            return ok(attach);
        }
        catch (Exception e)
        {

            return error(e);
        }
    }

    /**
     * 修改附件{attach_id}的名称等。
     * 
     * @PUT
     * @Path ("/{attach_id}")
     * @param name
     *            附件名称
     * @param description
     *            附件描述
     * @return
     * @author dengjinye,2010-10-29
     */
    @PUT
    @Path("/{attach_id}")
    public String updateAttach(@PathParam("attach_id")
    long attachId, @FormParam("name")
    String name, @FormParam("description")
    String description, MultivaluedMap<String, String> formParams)
    {
        try
        {
            
            Attach attach = new Attach(qr, owner);
            attach.setAttachId(attachId);
            StringBuilder updateColumn = new StringBuilder();
            if (Format.noEmpty(name))
            {
                updateColumn.append(Attach.NAME);
                attach.setName(name);
            }
            String seq = formParams.getFirst(Attach.SEQUENCE);
            if (Format.noEmpty(seq))
            {
                if (updateColumn.length() > 0)
                    updateColumn.append(",");
                updateColumn.append(Attach.SEQUENCE);
                attach.setSequence(Integer.parseInt(seq));
            }
            if (Format.noEmpty(description))
            {
                if (updateColumn.length() > 0)
                    updateColumn.append(",");
                updateColumn.append(Attach.DESCRIPTION);
                attach.setDescription(description);
            }
            
            if (updateColumn.length() == 0)
                return ok(false);
            
            attach.update(updateColumn.toString());
            
            attach.load(Attach.ENTITY_ID);
            addUrlLog(String.valueOf(attach.getAttachId()),String.valueOf(attach.getEntityId()));
            return ok(true);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }
}
